/*
 * Copyright 2007 The Bit Bakery (Kurt Christensen)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bitbakery.plugin.lisp.navigation;

import com.intellij.navigation.ChooseByNameContributor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO: Describe the role(s) and responsibilit(ies)
 */
public abstract class LispChooser implements ChooseByNameContributor {

    public String[] getNames(Project project, boolean includeNonProjectItems) {
        // TODO - Get list of defuns and defmacros (anything else?)
        List<String> names = gatherNames(project.getProjectFile().getParent());
        return names.toArray(new String[names.size()]);
    }

    private List<String> gatherNames(VirtualFile directory) {
        List<String> names = new ArrayList<String>();
        if (directory != null) {
            for (VirtualFile file : directory.getChildren()) {
                if (file.isDirectory()) {
                    // TODO - We actually need to get the PSI from th efile to get the defuns, etc.
                    names.addAll(gatherNames(file));
                } else if (isLisp(file)) {
                    names.add(file.getName());
                }
            }
        }
        return names;
    }

    private boolean isLisp(VirtualFile file) {
        String ext = file.getExtension();
        return "lisp".equalsIgnoreCase(ext) || "lisp".equalsIgnoreCase(ext);
    }
}
